/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PropertyGet.java
 *
 * Created on 13-dic-2011, 12.48.51
 */

package sybel.pan;

import javax.swing.JPanel;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import sybel.DOMPanelInterface;

/**
 *
 * @author Stefano
 */
public class PropertyGet extends javax.swing.JPanel implements DOMPanelInterface{

    /** Creates new form PropertyGet */
    public PropertyGet(Node parent) {
        Parent = parent;
        NodeList nl = ((Element)parent).getElementsByTagName("propertyGet");
        propertyGet = (Element)nl.item(0);
        initComponents();
        if(propertyGet != null)
            initPanel();
        annidation++;
    }

    public PropertyGet(Node parent,int count) {
        Parent = parent;
        NodeList nl = ((Element)parent).getElementsByTagName("propertyGet");
        propertyGet = (Element)nl.item(0);
        initComponents();
        if(propertyGet != null)
            initPanel();
        annidation++;
        annidation = count +1;
        if(annidation == 3)
            jCBEntity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "entity","variable" }));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextProperty = new javax.swing.JTextField();
        jCBEntity = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Property Get"));

        jLabel3.setText("Entity:");

        jLabel1.setText("*property:");

        jCBEntity.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose one...", "entity", "variable", "action execute", "property Get" }));
        jCBEntity.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBEntityItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextProperty, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBEntity, 0, 98, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextProperty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jCBEntity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCBEntityItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBEntityItemStateChanged
        EntitySelector();
}//GEN-LAST:event_jCBEntityItemStateChanged

    public void initPanel(){
        jTextProperty.setText(propertyGet.getAttribute("property"));

        NodeList nl = propertyGet.getElementsByTagName("entity");
        if(nl.item(0) != null)
            setEntityBody(1);

        nl = propertyGet.getElementsByTagName("variable");
        if(nl.item(0) != null)
            setEntityBody(2);

        nl = propertyGet.getElementsByTagName("actionExecute");
        if(nl.item(0) != null)
            setEntityBody(3);

        nl = propertyGet.getElementsByTagName("propertyGet");
        if(nl.item(0) != null)
            setEntityBody(4);

        EntitySelector();
    }

    /**
     * Metodo per preimpostare il contenuto del pannello entity attraverso il passaggio di
     * un intero identificante il corpo del propertyGet tra entity, variable, actionExecute e propertyGet
     * @param h passare: 1 per actionExecute, 2 propertyGet, 3 propertyGet
     *
     */
    public void setEntityBody(int h){
        switch(h){
            case 1:// Entity
                jCBEntity.setSelectedIndex(h);
            case 2:// Variable
                jCBEntity.setSelectedIndex(h);
            case 3:// actionExecute
                jCBEntity.setSelectedIndex(h);
            case 4:// propertyGet
                jCBEntity.setSelectedIndex(h);
            default:
                jCBEntity.setSelectedIndex(0);
        }
    }


    private void EntitySelector(){
        switch(jCBEntity.getSelectedIndex()){
            // Entity
            case 1:
                Entity entity;
                if(propertyGet == null){
                    entity = new Entity(Parent);
                }else{
                    entity = new Entity(propertyGet);
                }
                setEntityPanel(entity);
                break;

             // Variable
            case 2:
                Variable variable;
                if(propertyGet == null){
                    variable = new Variable(Parent);
                }else{
                    variable = new Variable(propertyGet);
                }
                setEntityPanel(variable);
                break;

            // Action Execute
            case 3:
                ActionExecute AEPanel;
                if(propertyGet == null){
                    AEPanel = new ActionExecute(Parent,annidation);
                }else{
                    AEPanel = new ActionExecute(propertyGet,annidation);
                }
                setEntityPanel(AEPanel);
                break;

            // Property Get
            case 4:
                PropertyGet propertyget;
                if(propertyGet == null){
                    propertyget = new PropertyGet(Parent,annidation);
                }else{
                    propertyget = new PropertyGet(propertyGet,annidation);
                }
                setEntityPanel(propertyget);
                break;
        }
    }

    private void setEntityPanel(JPanel entity){
        jPanel1.removeAll();
        javax.swing.GroupLayout layout2 = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(layout2);
        layout2.setHorizontalGroup(
                layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                );
        layout2.setVerticalGroup(
                layout2.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(entity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                );

        // rendo visibile il nuovo pannello
        jPanel1.repaint();
        jPanel1.setVisible(true);
    }

    public Node saveData(){
        Document doc = Parent.getOwnerDocument();
        propertyGet = doc.createElement("propertyGet");
        propertyGet.setAttribute("property",jTextProperty.getText());

        int temp = jCBEntity.getSelectedIndex();
        switch(temp){
            case 1:// Entity
                Entity ent = (Entity)jPanel1.getComponents()[0];
                propertyGet.appendChild(ent.saveData());
                break;

            case 2:// Variable
                Variable var = (Variable)jPanel1.getComponents()[0];
                propertyGet.appendChild(var.saveData());
                break;

            case 3:// ActionExecute
                ActionExecute ae = (ActionExecute)jPanel1.getComponents()[0];
                propertyGet.appendChild(ae.saveData());
                break;

            case 4:// PropertyGet
                PropertyGet propget = (PropertyGet)jPanel1.getComponents()[0];
                propertyGet.appendChild(propget.saveData());
                break;
        }
        return (Node)propertyGet;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jCBEntity;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextProperty;
    // End of variables declaration//GEN-END:variables
    private int annidation = 0;
    private Element propertyGet;
    private Node Parent;
}
